# CoffeeScript と gl.enchant.js を使って 3D ゲームを作っていきます
# 10分ほどお付き合い下さい

# この下にコードを書いていきます

# 定数
SCREEN_SIZE   = 640
PLAYER_MODEL  = "model/player.mqo"
ENEMY_MODEL   = "model/virus.mqo"

# おまじない
enchant()

# ゲーム生成
game = new Game(SCREEN_SIZE, SCREEN_SIZE)
game.preload PLAYER_MODEL, ENEMY_MODEL

# 初期化
game.onload = ->
  # シーン
  scene = game.rootScene
  
  # シーン3D
  scene3d = new Scene3D()
  
  # ライト
  light = new DirectionalLight()
  light.color = [1.0, 1.0, 1.0]
  scene3d.setDirectionalLight light
  
  # カメラ
  camera = new Camera3D()
  camera.x = 0
  camera.y = 1
  camera.z = 15
  camera.centerY = -1
  scene3d.setCamera camera
  
  # プレイヤー
  player = new Sprite3D()
  scene3d.addChild player
  player.set game.assets[PLAYER_MODEL]
  
  # タッチ
  scene.ontouchmove = (e) ->
    player.x = (e.x/SCREEN_SIZE) * 4-2
    player.y = -((e.y/SCREEN_SIZE) * 4-2)
  
  # シーン切替時処理
  scene.onenter = ->
    game.frame = 0
  
  # 更新
  scene.onenterframe = ->
    # 敵生成
    if game.frame % 10 is 0
      enemy = new Cube()
      scene3d.addChild enemy
      enemy.set game.assets[ENEMY_MODEL]
      
      enemy.x = Math.random()*4-2
      enemy.y = Math.random()*4-2
      enemy.z = -100
      
      enemy.onenterframe = ->
        @z += 1
        @parentNode.removeChild this if @z > 15
        
    # 衝突判定
    i = 0
    len = scene3d.childNodes.length
    
    while i<len
      elm = scene3d.childNodes[i]
      if elm isnt player and player.intersect(elm) is true
        seconds = game.frame / game.fps
        score = seconds.toFixed()*100
        console.log score, seconds
        game.end score, score + "秒耐えました"
      ++i
    
  
  
  

# ゲーム開始
game.start()

# 完成です.
# 最後まで見ていただきありがとうございました.
# ではまたノシ



